package www.alterzhang.cn.webservice;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.util.concurrent.ListenableFuture;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.alterzhang.cn.service.AsyncService;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * Created by Administrator on 2018/4/14 0014.
 */
@RestController
@EnableAsync
public class HelloWorldController {

    @Autowired
    private AsyncService asyncService;

    @RequestMapping("/getStr")
    public String getStr() throws InterruptedException, ExecutionException {
        Integer TWO = asyncService.get2();
        System.out.println(System.currentTimeMillis() + "======get2 HelloWorldController======");
        ListenableFuture THREE = asyncService.get3();
        System.out.println(System.currentTimeMillis() + "======get3 HelloWorldController======");
        CompletableFuture FOUR = asyncService.get4();
        System.out.println(System.currentTimeMillis() + "======get4 HelloWorldController======");
        Future FIVE = asyncService.get5();
        System.out.println(System.currentTimeMillis() + "======get5 HelloWorldController======");
        Integer SIX = asyncService.get6();
        System.out.println(System.currentTimeMillis() + "======get6 HelloWorldController======");

        if (THREE.isDone()) {
            System.out.println(THREE.get());
        }
        if (FOUR.isDone()) {
            System.out.println(FOUR.get());
        }
        if (FIVE.isDone()) {
            System.out.println(FIVE.get());
        }
        return "success";
    }

    @RequestMapping("/getMap")
    public Map<String, Object> getMap() {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("result", "success");
        int i = 1 / 0;
        return map;
    }

}
